/**
 * @param {number[]} satisfaction
 * @return {number}
 */
var maxSatisfaction = function(satisfaction) {
    satisfaction.sort(function compareNumbers(a, b) {
        return a - b;
    })
    console.log(satisfaction)
    var total = 0
    var arr = []
    let i = 0 // 最后一个负数
    satisfaction.forEach((item, index) => {
        if (item < 0) {
            i = index
        }
        // if (item >= 0) {
        //     arr.push(item)
        // }
    })

    if (i === satisfaction.length - 1) return 0

    var total = 0
    var arr = []
    for (var p = 0, length = satisfaction.length; p < length; p++) {
        arr.push(qqq(satisfaction, p))
    }
    console.log(arr)

    // arr.forEach((item, index) => {
    //     total += item * (index + 1)
    // })
    return total
};

function qqq(satisfaction, x) {
    var total = 0
    for (; x < satisfaction.length; x++) {
        total += (x + 1) * satisfaction[x]
    }
    return total
}

console.log(maxSatisfaction([-1,-8,0,5,-9]));